package com.web.music.portal.mvc;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.bind.ServletRequestDataBinder;

import com.web.music.interfaces.AlbumService;
import com.web.music.interfaces.ArtistService;
import com.web.music.interfaces.GenreService;
import com.web.music.interfaces.SecurityController;
import com.web.music.model.Album;
import com.web.music.model.Artist;
import com.web.music.model.Genre;
import com.web.music.model.Singer;


public class AlbumFormController extends SimpleFormController {

	private AlbumService albumService;
	private ArtistService artistService;
	private GenreService genreService;
	private SecurityController securityController;
	
	public AlbumFormController() {
		setCommandClass(Album.class);
		setSessionForm(true);
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request) {		
		Album album = new Album();
		Singer dummySinger = new Singer();
		album.setArtist(dummySinger); //To avoid Value of nested property 'artist' is null
		
		album.setCreatedBy(securityController.getSessionUser(request));			
		// execute the following code only when going to display a form
		if (!isFormSubmission(request)){	
			String idString = request.getParameter("id");
			if (idString != null && !idString.equals("")) {
				long id = Long.parseLong(request.getParameter("id"));
				album = albumService.getById(id);
			}			
		}		
		securityController.updateSecurityStatus(request, album);
		return album;		
	}

	
	@Override
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Artist.class, new ArtistPropertyEditor(artistService));
		binder.registerCustomEditor(Genre.class, new GenrePropertyEditor(genreService));
		super.initBinder(request, binder);
	}
	
	@Override
    protected Map<String,Object> referenceData(HttpServletRequest request) throws Exception 
    {
        Map<String,Object> refData = new HashMap<String,Object>();
        List<Artist> artists = artistService.findAll();
        List<Genre> genres = genreService.findAll();
        refData.put("artists", artists);
        refData.put("genres", genres);
        return refData;
    }
	
	@Override
	public void doSubmitAction(Object object){
		Album album = (Album)object;	
		albumService.save(album);	
	}

	public AlbumService getAlbumService() {
		return albumService;
	}

	public void setAlbumService(AlbumService albumService) {
		this.albumService = albumService;
	}
	public ArtistService getArtistService() {
		return artistService;
	}

	public void setArtistService(ArtistService artistService) {
		this.artistService = artistService;
	}
	
	public GenreService getGenreService() {
		return genreService;
	}

	public void setGenreService(GenreService genreService) {
		this.genreService = genreService;
	}	
	public SecurityController getSecurityController() {
		return securityController;
	}

	public void setSecurityController(
			SecurityController securityController) {
		this.securityController = securityController;
	}		
}
